package jk.fox.spring.bootstrap;

import jk.fox.spring.container.SpringRpcContainer;
import jk.fox.common.payload.container.RpcContainer;
import jk.fox.engine.RpcNodeDefinition;
import jk.fox.engine.context.DefaultRpcContext;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.util.Assert;

/**
 * @author cuichao
 * @Description: SpringRpcContext
 * @Date: create in 2020/5/28 22:17
 */
public class SpringRpcContext extends DefaultRpcContext {

    public SpringRpcContext(RpcContainer container) {
        super(container);
        Assert.isInstanceOf(SpringRpcContainer.class, container, String.format("%s requires a %s", SpringRpcContext.class.getName(),SpringRpcContext.class.getName()));

    }

    @Override
    public Object registryService(String bean, Class<?> cls,Object object) {
        //spring-ioc容器对Service 自动注入不需无需手动载入bean
        putDefinition(bean,cls,true);
        return object;
    }

    @Override
    public Object registryReference(String bean, Class<?> cls,Object object) {
        AnnotationAttributes attributes = (AnnotationAttributes)object;
        //得到Rpc消费者代理对象并返回给spring容器
        RpcNodeDefinition definition = putDefinition(bean, cls, false,attributes);
        Object proxy = invocation.proxy(definition);
        container.addReference(bean,proxy);
        return proxy;
    }





}
